<!-- 新增编辑车辆 -->
<template>
  <div>
    <el-dialog :title="titleInfo.text" :visible.sync="dialogFormVisible" custom-class="way" width="600px">
      <el-form ref="dataForm" :rules="ruleInline" :model="formBase" label-position="right" label-width="110px" style="width: 100%">
        <el-form-item label="车辆牌号" prop="vehicleNumber">
          <el-input v-model="formBase.vehicleNumber" placeholder="请输入车辆牌号" maxlength="15"/>
        </el-form-item>
        <el-form-item label="车辆型号" prop="vehicleTypeId">
          <el-select v-model="formBase.vehicleTypeId" value-key="id" placeholder="请选择车辆型号" style="width: 100%" clearable @change="chooseVehicle()">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="车型编码" prop="vehicleCode">
          <el-input v-model="formBase.vehicleCode" maxlength="10" :disabled="true"/>
        </el-form-item>
        <el-form-item label="品牌logo" prop="vehicleLogo">
          <el-input v-model="formBase.vehicleLogo" placeholder="请输入品牌logo" maxlength="10"/>
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input v-model="formBase.remark" placeholder="请输入说明" maxlength="200"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button class="save-btn" @click="createData">确认</el-button>
        <el-button class="cancel-btn" @click="dialogFormH">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { truckTypeCommonList } from '@/api/common'
import { truckTypeSaveUpdate } from '@/api/transit'
export default {
  name: 'VehicleAdd',
  props: {
    titleInfo: {
      type: Object,
      required: true
    },
    formBase: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      // 是否可以编辑
      options: '',
      disabled: true,
      activeClass: 'active',
      disactiveClass: 'disactive',
      dialogFormVisible: false,
      // 表单验证
      ruleInline: {
        vehicleNumber: [{ required: true, message: '车牌号不能为空', trigger: 'blur' }],
        remark: [{ required: true, message: '说明不能为空', trigger: 'blur' }]
      }
    }
  },
  // 创建完毕状态
  created() {
    this.getTruckTypeList()
  },
  // 组件更新
  updated: function () {},
  methods: {
    // 弹层显示
    dialogFormV() {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH() {
      this.$refs['dataForm'].resetFields()
      this.dialogFormVisible = false
    },
    // 获取运输车型列表数据
    async getTruckTypeList() {
      const { data: res } = await truckTypeCommonList()
      // 获取数据给options
      this.options = res
    },
    // 表单提交
    createData() {
      this.$refs['dataForm'].validate(async (valid) => {
        if (valid) {
          const data = {
            id: this.formBase.id,
            vehicleNumber: this.formBase.vehicleNumber,
            vehicleTypeId: this.formBase.vehicleTypeId,
            vehicleTypeName: this.formBase.vehicleTypeName,            
            vehicleCode: this.formBase.vehicleCode,
            vehicleLogo: this.formBase.vehicleLogo,
            remark: this.formBase.remark
          }
          if (this.formBase.id) {
            await truckTypeSaveUpdate(data).then((res) => {
              console.log(res);
              if (String(res.code) === '200') {
                this.$message.success('修改成功')
                this.$emit('newDataes', this.formBase)
                this.dialogFormH()
              } else {
                this.$message.error('修改失败')
              }
            })
          } else {
            await truckTypeSaveUpdate(data).then((res) => {
              if (res.data === true) {
                this.$message.success('新增成功')
                this.$emit('newDataes', this.formBase)
                this.dialogFormH()
              } else {
                this.$message.error('新增失败')
              }
            })
          }
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    },
    //选择车型
    chooseVehicle() {
      const vehicle = this.options.find(option => option.id === this.formBase.vehicleTypeId)
      this.formBase.vehicleCode = vehicle.code
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
</style>
